<template>
    <div class="order-detail-wrap">
        <div class="top-status" v-if="info.astatus === '8'">
            <div class="flex al-c"><i class="iconfont icon-wallet"></i>
                待付款 &emsp;
                <small>剩余时间 {{diffTime}}</small>
            </div>
        </div>
        <div class="tishi" v-if="info.astatus === '8'">注：30分钟内未支付的预约课程将会自动删除</div>
        <div class="c-info">
            <div class="flex cur name al-c" @click="$router.push('/course/' + info.course.id)">
                <l-image :src="info.course.cPHOTO.split(',')[0]" width="100" height="100"></l-image>
                <div style="padding-left: .8em;">
                    <div style="margin-bottom: 10px;">{{info.course.cname}}</div>
                </div>
            </div>
            <ul>
                <li>
                    <span>订单编号</span> {{info.id}}
                </li>
                <li>
                    <span>预订日期</span> {{info.orderpay.ordertime}}}
                </li>
                <li>
                    <span>课程编号</span> {{info.cid}}
                </li>

                <li>
                    <span>应付金额</span>
                    <div class="price">￥{{info.orderpay.paymoney}}</div>
                </li>
            </ul>
        </div>
        <div class="o-card">
            <div class="title">行程信息</div>
            <ul>
                <li v-if="qrInfo">
                    <span>券&emsp;&emsp;码</span> {{qrInfo.qrcode}}
                </li>
                <li v-if="qrInfo">
                    <span>券码状态</span> {{qrInfo.qstatus | qrType}}
                </li>
                <li>
                    <span>行程日期 </span>{{info.courseTime}}
                </li>
                <li>
                    <span>报名类型 </span>{{info.courseType | coursePriceType}}
                </li>
                <li>
                    <span>报名人数 </span>{{info.anumber}} 人
                </li>
            </ul>
        </div>
        <div class="o-card">
            <div class="title">预约信息</div>
            <ul>
                <li>
                    <span>姓&emsp;&emsp;名</span>{{info.aNAME}}
                </li>
                <li>
                    <span>身份证号</span>{{info.aid}}
                </li>
                <li>
                    <span>联系电话</span>{{info.aPHONE}}
                </li>
                <li>
                    <span>备&emsp;&emsp;注</span>{{info.field}}
                </li>
                <!--<li>
                    <span>电子邮箱</span>
                </li>-->
            </ul>
        </div>
        <div class="fix-bar flex al-c jc-sb">
            <div class="price">
                <small>总价</small>
                ￥{{info.orderpay.paymoney}}
            </div>
            <div class="btns">
                <!--                <l-button class="cancel">取消订单</l-button>-->
                <l-button v-if="info.astatus === '8'"
                           :to="`/course-form/${info.course.id}?orderid=${info.id}&payid=${info.orderpay.id}&cid=${info.cid}&num=${info.aNUMBER}&courseType=${info.courseType}`">
                    继续支付
                </l-button>
                <l-button v-if="info.astatus === '0'" @click="handlerRefund(info.id)">
                    申请退款
                </l-button>
            </div>
        </div>
    </div>
</template>

<script>
    import { Dialog } from 'vant'
    export default {
        name: 'OrderDetail',
        data() {
            return {
                info: null,
                qrInfo: null,
                courseInfo: null,
                diffTime: '00 : 30 : 00',
                timer: null
            }
        },
        computed: {},
        async created() {
        },
        async mounted() {
            this.id = this.$route.params.id
            this.getInfo()
        },
        methods: {
            async getInfo() {
                try {
                    const {data} = await this.$api.order.detail(this.id)
                    console.log(data)
                    data.courseTime = this.$utils.dayjs(data.coursetime.runtime).format('YYYY年MM月DD日 HH:mm')
                    clearInterval(this.timer)
                    this.info = data
                    if (data.astatus === '0') {
                        this.$api.order.qrCode(this.id).then(res => {
                            this.qrInfo = res.data.qrCode
                        })
                    }
                    this.timer = setInterval(() => {
                        const diff = this.getTime()
                        if (diff === 0) {
                            clearInterval(this.timer)
                            this.getInfo()
                        }
                    }, 1000)
                } catch (e) {
                    console.error(e)
                }
            },
            async handlerRefund(v) {
                try {
                    await Dialog.confirm({
                        title: '提示',
                        message: '您确认要退款吗'
                    })
                    await this.$api.order.refund(v)
                    this.$toast.success('退款申请已提交')
                    this.getInfo()
                }catch (e) {

                }
            },
            getTime() {
                const dayjs = this.$utils.dayjs
                const oday = dayjs(this.info.createdTime)
                const a = (30 * 60) - dayjs().diff(oday, 'second')
                let m = (a / 60).toString().split('.')[0]
                let s = a - (m * 60)
                if (m < 10) {
                    m = '0' + m
                }
                if (s < 10) {
                    s = '0' + s
                }
                this.diffTime = `00 : ${m} : ${s}`
                return a
            }
        }
    }
</script>

<style lang="scss">
    @import "../../../../style/var";

    .order-detail-wrap {
        padding-bottom: 100px;

        .tishi {
            background-color: #fff;
            padding: 30px 40px;
            margin-bottom: 10px;
            color: $main-color-price;
        }

        .top-status {
            background-color: $main-color;
            color: #fff;
            padding: 40px;
            font-size: 36px;

            i {
                font-size: 50px;
                margin-right: 10px;
            }
        }

        .c-info {
            background-color: #fff;
            padding: 0 40px;

            .name {
                border-bottom: 1px solid #eee;
                padding: 20px 0;
                line-height: 1.6;
            }

            ul {
                padding: 20px 0;
            }

            li {
                display: flex;
                align-items: center;
                line-height: 1.8;

                span {
                    display: inline-block;
                    flex: 0 0 170px;
                    color: #666;
                }

                .price {
                    color: $main-color;
                }
            }
        }

        .o-card {
            margin: 20px;
            background-color: #fff;
            border-radius: 20px;
            overflow: hidden;

            .title {
                height: 80px;
                background-color: #dddddd;
                display: flex;
                align-items: center;
                font-size: 32px;
                padding-left: 20px;
            }

            ul {
                padding: 20px;

                li {
                    display: flex;
                    line-height: 1.7;

                    span {
                        flex: 0 0 170px;
                        color: #666;
                    }
                }
            }
        }

        .fix-bar {
            position: fixed;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #fff;
            padding: 0 20px;
            border-top: 1px solid #eee;
            height: 100px;

            .price {
                color: $main-color;

                small {
                    color: #333;
                }
            }

            .btns {
                button {
                    background: $main-color;
                    color: #fff;
                    margin-left: 10px;
                }

                .cancel {
                    background-color: #fff;
                    color: $main-color;
                }
            }
        }
    }
</style>
